﻿namespace RuoTeng.Win32.Control
{
    using System;
    using System.Runtime.InteropServices;
    using System.Collections.Generic;

    class Edit:RuoTeng.Win32.Message
    {
        public const int EM_GETSEL = 0x00B0;
        public const int EM_SETSEL = 0x00B1;
        public const int EM_GETRECT = 0x00B2;
        public const int EM_SETRECT = 0x00B3;
        public const int EM_SETRECTNP = 0x00B4;
        public const int EM_SCROLL = 0x00B5;
        public const int EM_LINESCROLL = 0x00B6;
        public const int EM_SCROLLCARET = 0x00B7;
        public const int EM_GETMODIFY = 0x00B8;
        public const int EM_SETMODIFY = 0x00B9;
        public const int EM_GETLINECOUNT = 0x00BA;
        public const int EM_LINEINDEX = 0x00BB;
        public const int EM_SETHANDLE = 0x00BC;
        public const int EM_GETHANDLE = 0x00BD;
        public const int EM_GETTHUMB = 0x00BE;
        public const int EM_LINELENGTH = 0x00C1;
        public const int EM_REPLACESEL = 0x00C2;
        public const int EM_GETLINE = 0x00C4;
        public const int EM_LIMITTEXT = 0x00C5;
        public const int EM_CANUNDO = 0x00C6;
        public const int EM_UNDO = 0x00C7;
        public const int EM_FMTLINES = 0x00C8;
        public const int EM_LINEFROMCHAR = 0x00C9;
        public const int EM_SETTABSTOPS = 0x00CB;
        public const int EM_SETPASSWORDCHAR = 0x00CC;
        public const int EM_EMPTYUNDOBUFFER = 0x00CD;
        public const int EM_GETFIRSTVISIBLELINE = 0x00CE;
        public const int EM_SETREADONLY = 0x00CF;
        public const int EM_SETWORDBREAKPROC = 0x00D0;
        public const int EM_GETWORDBREAKPROC = 0x00D1;
        public const int EM_GETPASSWORDCHAR = 0x00D2;
        public const int EM_SETMARGINS = 0x00D3;
        public const int EM_GETMARGINS = 0x00D4;
        public const int EM_SETLIMITTEXT = EM_LIMITTEXT;   /* ;win40 Name change */
        public const int EM_GETLIMITTEXT = 0x00D5;
        public const int EM_POSFROMCHAR = 0x00D6;
        public const int EM_CHARFROMPOS = 0x00D7;
    
        public static string GetText(IntPtr hWnd,int startLine,int endLine)
        {   
            if(endLine==-1)
            {
               endLine=SendMessage(hWnd, EM_GETLINECOUNT, 0, 0);
            }
            string text="";
            for (int i = startLine; i < endLine;i++)
            {
                int length = SendMessage(hWnd, EM_LINELENGTH, i, 0);
                IntPtr intPtr = (IntPtr)Marshal.AllocHGlobal(length);
                SendMessage(hWnd, EM_GETLINE, i, intPtr);
                text += Marshal.PtrToStringAnsi(intPtr);
                Marshal.FreeHGlobal(intPtr);
            }
            return text;
        }

        public static string GetText(IntPtr hWnd)
        {
            return GetText(hWnd,0, -1);
        }

        public static void SetText(IntPtr hWnd,string text, int startLine, int endLine)
        {
            if (endLine == -1)
            {
                endLine = SendMessage(hWnd, EM_GETLINECOUNT, 0, 0);
            }
            int length = 0;
            for (int i = startLine; i < endLine; i++)
            {
                length += SendMessage(hWnd, EM_LINELENGTH, 0, 0);
            }
            PostMessage(hWnd, EM_SETSEL, 0, 0);
            SendMessage(hWnd, EM_SETSEL, 0, length);
            IntPtr intPtr = Marshal.StringToHGlobalAnsi(text);
            SendMessage(hWnd, EM_REPLACESEL, 1, intPtr);
            Marshal.FreeHGlobal(intPtr);
        }

        public static void SetText(IntPtr hWnd, string text)
        {
            SetText(hWnd, text, 0, -1);
        }
    }
}
